const utils = require('../../utils/util');
let db = wx.cloud.database({
  env: 'mini-cloud-8gonte8a94b9dcfb'
});
let user = db.collection('db_user');
Page({
  data: {
    avatarUrl:"",
    nickName:"",
    islogin:false,
    canIUseGetUserProfile: true,
  },
  mineRecord(){
    if(this.data.islogin==false){
      this.getUser()
    }else{
      wx.navigateTo({
        url: `../myrecord/record`,
      })
    }
  },
  suggest(){
    wx.showModal({
      title: 'APP建议',
      content: '该app只用于给用户提供阅读服务',
      success(res){
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
  },
  mineCollect(){
    if(this.data.islogin==false){
      this.getUser()
    }else{
      wx.navigateTo({
        url: `../mycollect/collect`,
      })
    }
  },
  quit(){
    wx.removeStorageSync("history")
    wx.removeStorageSync('avatarUrl');
    wx.removeStorageSync('nickName');
    wx.removeStorageSync('openid');
    wx.removeStorageSync('user_create_time');
    wx.removeStorageSync('userinfo');
    wx.removeStorageSync('isHas');
    this.setData({
      islogin:false,
    })
  },
  getUser(){
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        
        let avatarUrl="cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/header.jpeg"
        let nickName=res.userInfo.nickName
        console.log(avatarUrl,nickName);
        this.setData({
          avatarUrl,
          nickName,
          islogin:true
        })
        this.getUserInfoFunc(nickName, avatarUrl);
      }
    })
  },
  getUserInfoFunc(nickName, avatarUrl){
    let dt = new Date();
    // console.log('时间:',utils.formatTime(dt));
    // console.log(nickName, avatarUrl);
    wx.cloud.callFunction({
      name:"user",
      data: {
        user_create_time: utils.formatTime(dt),
        nickName,
        avatarUrl
      },
      success(content) {
        console.log(content);
        let {
          errMsg,
          result
        } = content;
        if (errMsg == 'cloud.callFunction:ok') {
          if (result.code == 200) {
            let {
              userInfo: {
                nickName,
                avatarUrl,
                openid,
                user_create_time
              }
            } = result;
            wx.setStorageSync('avatarUrl', avatarUrl);
            wx.setStorageSync('nickName', nickName);
            wx.setStorageSync('openid', openid);
            wx.setStorageSync('user_create_time', user_create_time);
            wx.setStorageSync('userinfo', JSON.stringify(result.userInfo));
            wx.setStorageSync('isHas', 1);
            user.where({user_id:openid}).get().then(res=>{
              if(res.data.length>0){
                return
              }else{
                user.add({data:{
                  user_id: openid,
                  // _openid: openid,
                  user_nickname: nickName,
                  user_create_time: user_create_time,
                  user_avatar: avatarUrl
                }})
              }
            })   
          }else {
            wx.showToast({
              title: '注册用户信息失败',
              icon: 'error'
            })
          }
        }
      }
    })
  },
  onLoad(options) {
    let isHas = wx.getStorageSync('isHas');
    if (isHas) {
      // 获取本地储存的数据
      let userinfo = JSON.parse( wx.getStorageSync('userinfo'))
      let {nickName,avatarUrl}=userinfo
      this.setData({
        canIUseGetUserProfile: false, 
        nickName,
        avatarUrl,
        islogin:true
      })
  }else{
    this.setData({
      canIUseGetUserProfile: true
    })
  }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})